package com.cd.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.cd.bean.Contact;
import com.cd.bean.Message;
import com.cd.bean.News;
import com.cd.bean.Reply;
import com.cd.bean.Technology;
import com.cd.service.message.MessageService;
import com.cd.service.reply.ReplyService;
import com.cd.utils.RandomUtils;
import com.cd.utils.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 网上留言
 *@Author zhk
 *@Date 2017-12-20
 **
 */
@RequestMapping("/msg")
@Controller
public class MessageController {

	@Autowired
	MessageService messageService;
	@Autowired
	ReplyService replyService;
	
	/**
	 * 新增
	 * @param name
	 * @param email
	 * @param phone
	 * @param area
	 * @param type
	 * @param title
	 * @param content
	 * @param pic
	 * @param face
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(@RequestParam(value="name")String name,@RequestParam(value="email")String email,@RequestParam(value="phone")String phone,
			@RequestParam(value="area")String area,@RequestParam(value="type")String type,@RequestParam(value="title")String title,
			@RequestParam(value="content")String content,@RequestParam(value="pic")String pic,@RequestParam(value="face")String face) throws Exception {
		Message msg = new Message();
		//int total = messageService.size();
		Reply reply = new Reply();
		reply.setREPLY_ID("0");//新增留言时，默认设置回复内容为空
		msg.setReply(reply);
		//msg.setMESSAGE_ID(total+1);
		msg.setNICKNAME(name);
		msg.setEMAIL(email);
		msg.setAREA(area);
		msg.setMESSAGE_TYPE(type);
		msg.setMESSAGE_TITLE(title);
		msg.setPHONE(phone);
		msg.setMESSAGE_CONTENT(content);
		msg.setMESSAGE_PIC(pic);
		msg.setMESSAGE_FACE(face);
		msg.setMESSAGE_DATE(new Date());
		messageService.save(msg);
		return  "redirect:/msg/listmsg.do";
	}
	

	/**
	 * 查询（带分页）
	 * @param pn
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listmsg")
	public ModelAndView findAllMessage(@RequestParam(value = "pn",defaultValue="1")Integer pn) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageHelper.startPage(pn, 15);//开始分页
	    List<Message> list = messageService.listAll();
		PageInfo page = new PageInfo(list,5);//设置分页信息
		mv.addObject("pageInfo", page);
		mv.setViewName("info/wsly/wsly");
		return mv;
	}
	
	/**
	 * 通过id查询
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findById")
	public ModelAndView findById(@RequestParam(value = "id") Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		Message msg = null;
		Message nextmsg = null;
		Message premsg = null;
		int total = messageService.size();//获取总记录
		int nextId = id+1;
		int preId = id-1;
		if(id <= total){
			if(id >= 1){
				msg = messageService.findById(id);//当前条
				premsg = messageService.findById(preId);//前一条
				nextmsg = messageService.findById(nextId);//下一条	
			}else {
				msg = messageService.findById(id+1);
			}
		}else{
			msg = messageService.findById(id-1);
		}
		mv.addObject("msg", msg);
		mv.addObject("premsg", premsg);
		mv.addObject("nextmsg", nextmsg);
		mv.setViewName("info/wsly/wsly_show");
		return mv;
	}
	
	/**
	 * 去增加页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addmsg")
	public ModelAndView goAddMessage() throws Exception{
		ModelAndView mv = new ModelAndView();
		mv.setViewName("info/wsly/wsly_add");
		return mv;
	}
	
	/**
	 * 查询（带分页）
	 * @param pn
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(value = "pn",defaultValue="1")Integer pn) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageHelper.startPage(pn, 10);//开始分页
	    List<Message> list = messageService.listAll();
		PageInfo page = new PageInfo(list,5);//设置分页信息
		mv.addObject("pageInfo", page);
		mv.setViewName("admin/wsly/ly_list");
		return mv;
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public ModelAndView delete(@RequestParam(value="id")Integer id) throws Exception{
		Message msg =messageService.findById(id);
		ModelAndView mv = new ModelAndView();
		messageService.delete(id);
		mv.setViewName("redirect:list.do");
		return mv;
	}
	

	/**
	 * 去编辑界面
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goEdit")
	public ModelAndView goEdit(@RequestParam(value="id")Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		Message  msg = messageService.findById(id);
		mv.addObject("message", msg);
		mv.setViewName("admin/wsly/ly_edit");
		return mv;
	}

	/**
	 * （后台）编辑,回复留言
	 * @param id
	 * @param title
	 * @param name   
	 * @param phone
	 * @param email
	 * @param address
	 * @param postcode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/edit")
	public ModelAndView eidt(@RequestParam(value="MESSAGE_ID")Integer id,@RequestParam(value="REPLY_CONTENT")String replycontent) throws Exception {
			ModelAndView mv = new ModelAndView();
			Message msg = messageService.findById(id);
			String temp = UuidUtil.get32UUID();
			Reply reply=new Reply();
			reply.setREPLY_ID(temp);
			reply.setREPLY_CONTENT(replycontent);			
			msg.setReply(reply);
			msg.setMESSAGE_DATE(new Date());
			replyService.save(reply);//持久化回复内容
			messageService.edit(msg);//执行更新
			mv.setViewName("redirect:list.do");
		return mv;
	}
	/**
	 * 获取未回复的留言
	 * @param pn
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listNewmsg")
	public ModelAndView listWithoutReply(@RequestParam(value = "pn",defaultValue="1")Integer pn) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageHelper.startPage(pn, 10);//开始分页
		List<Message> list = messageService.findWithoutReply();
		PageInfo page = new PageInfo(list,5);//设置分页信息
		mv.addObject("pageInfo", page);
		mv.setViewName("admin/wsly/zxly_list");
		return mv;
	}
	/**
	 * 去查看内容界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goContent")
	public ModelAndView goContent(@RequestParam(value="id") Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		Message msg = messageService.findById(id);
		mv.addObject("msg", msg);
		mv.setViewName("admin/wsly/ly_content");
		return mv;
	}
	/**
	 * 
	 * 批量删除
	 * @param request
	 * @param response
	 */
	@RequestMapping("/del")  
	public void batchDeletes(HttpServletRequest request, HttpServletResponse response) {  
	    String items = request.getParameter("delitems");
	    //System.out.println(items);  
	    String[] strs = items.split(",");  
	  
	    for (int i = 0; i < strs.length; i++) {  
	        try {  
	            int a = Integer.parseInt(strs[i]);  
	            messageService.delete(a);  
	        } catch (Exception e) {  
	        }  
	    }  
	} 
}
